<?php

    $tpl = new SimpleTemplate(VIEW.'gestaoSistema/infoEmpresa.html');
    $inserir = new gestaoSistema;
    $func = new functions;
    $sel = new select;
      
    $action = $func->get_isset("action")->get();
    
    if($action == "inc"){    
        
        $tpl->block("BLOCK_INFO");
        $tpl->block("BLOCK_BUTTON_INC");
        
        $estados = $sel->uf();
        $atual = 'MG';

        foreach($estados as $uf){
            $tpl->UF = $uf;

            ($atual == $uf)? $tpl->SELECTED = 'selected' : $tpl->clear("SELECTED");

            $tpl->block("BLOCK_UF");
        } 
        
        if($_SERVER["REQUEST_METHOD"] == "POST"){
            $inserir->insert("inf_empresa", $_POST);
            $id = $inserir->last_insert_id();

            if($id)
               $func->go("home.php?pg=sucessEmpresa&action=inc&id=".$id);        
        }    
    }
    else if($action =="alt"){
        $tpl->block("BLOCK_BUTTON_ALT");
        
        $res = $inserir->select_unique("SELECT * FROM inf_empresa WHERE id=1");
        
        $tpl->CNPJ = $func->get_value($res["cnpj"])->format("cnpj")->get();
        $tpl->RAZAO = $res["razao_social"];
        $tpl->NOME_FANTASIA = $res["nome_fantasia"];
        $tpl->INSCRICAO_ESTADUAL = $res["inscricao_estadual"];
        $tpl->RUA = $res["rua"];
        $tpl->COMPLEMENTO = $res["complemento"];
        $tpl->NUMERO = $res["numero"];
        $tpl->BAIRRO = $res["bairro"];
        $tpl->CEP = $func->get_value($res["cep"])->format("cep")->get();
        $tpl->CIDADE = $res["cidade"];
        $tpl->TELEFONE = $func->get_value($res["telefone"])->format("phone")->get();
        $tpl->EMAIL = $res["email"];
        $tpl->SITE = $res["site"];
        
        $estados = $sel->uf();
        foreach($estados as $uf){
            $tpl->UF = $uf;

            ($res["uf"] == $uf)? $tpl->SELECTED = 'selected' : $tpl->clear("SELECTED");

            $tpl->block("BLOCK_UF");
        }           
        
         if($_SERVER["REQUEST_METHOD"] == "POST"){
            $inserir->update('inf_empresa', $_POST, 1);
            $id = $inserir->last_update_id();
               
               print("<script>location.href='home.php?pg=sucessEmpresa&action=alt&id=$id';</script>");  
              //header("Location: home.php?pg=sucessEmpresa&action=alt&id=".$id);        
        }         
    }

    $tpl->show();
?>